using HKB.Common;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace HKB.Web
{
	public class CardBll
	{
		private Conn conn = null;
		private Pagination _pageHand = null;
		protected Pagination pageHand
		{
			get
			{
				Pagination pageHand;
				if (this._pageHand == null)
				{
					this._pageHand = new Pagination();
					this._pageHand.SQL_CloseTablePre = false;
					this._pageHand.P_Object = Language.GetAdminMsg("Card/CardName");
					pageHand = this._pageHand;
				}
				else
				{
					pageHand = this._pageHand;
				}
				return pageHand;
			}
		}
		public CardBll()
		{
			this.conn = Text.GetComponent<Conn>("conn", HttpContext.Current.CurrentHandler);
		}
		public CardBll(Conn _conn)
		{
			this.conn = _conn;
		}
		public DataTable GetCards()
		{
			return this.conn.GetDataTable("SELETC * From [HL_Card] Order By CardID Desc");
		}
		public DataTable GetCards(int indexpage, int maxperpage, string WhereStr, string OrderByStr)
		{
			return this.pageHand.GetDataTable("HL_Card", indexpage, maxperpage, "CardID", WhereStr, OrderByStr, this.conn, null);
		}
		public string GetPagePanel(string linkhead)
		{
			this.pageHand.P_LinkHead = linkhead;
			return this.pageHand.ShowPagePanel();
		}
		public DataTable GetCardByNumPass(string CardNum, string CardPassword)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@CardNum", DbType.String, CardNum),
				this.conn.CreateParameter("@CardPassword", DbType.String, CardPassword)
			};
			return this.conn.GetDataTable("SELECT * FROM HL_Card WHERE (CardNum = @CardNum) AND (CardPassword = @CardPassword)", parameters);
		}
		public void DeleteCards(string cardids)
		{
			SqlParameter sqlParameter = new SqlParameter("@CARD_ID", DbType.String);
			sqlParameter.Value = cardids;
			this.conn.Execute("DeleteCards", new SqlParameter[]
			{
				sqlParameter
			});
		}
		public DataTable GetCardByCardID(int CardID)
		{
			return this.conn.GetDataTable("SELECT * FROM HL_Card WHERE (CardID = " + CardID + " )");
		}
		public bool SellCard(int CardID)
		{
			DataTable cardByCardID = this.GetCardByCardID(CardID);
			bool result;
			if (cardByCardID.Rows[0]["CardState"].ToString() == "3" && cardByCardID.Rows[0]["CardState"].ToString() == "2")
			{
				result = false;
			}
			else
			{
				int num = this.conn.Execute("UPDATE [HL_Card] Set CardState=2 Where CardState<>2 and CardState<>3 and CardID=" + CardID);
				result = (num == 1);
			}
			return result;
		}
		public bool SellCard(string CardIDs)
		{
			int num = this.conn.Execute("UPDATE [HL_Card] Set CardState=2 Where CardState<>2 and CardState<>3 and CardID in(" + CardIDs + ")");
			return num == 1;
		}
		public int AddCard(int CreateCardType, int CardQuantity, string CardNum, string CardPassword, int CardType, string CardAgentName, string CardValidNum, decimal CardMoney, string CardValidUnit, DateTime CardEndDate)
		{
			int num = 0;
			if (CreateCardType == 1)
			{
				CardQuantity = 1;
			}
			string str = DateTime.Now.ToString("yyyyMMddHHmmss");
			IDbDataParameter[] array = new IDbDataParameter[9];
			for (int i = 0; i < CardQuantity; i++)
			{
				if (CardType == 1)
				{
					CardNum = str + Text.MakeOutTradeNo();
					CardPassword = Text.MakePassword(8);
				}
				DateTime dateTime = Convert.ToDateTime(DateTime.Now.ToString());
				array[0] = this.conn.CreateParameter("@CardNum", DbType.String, CardNum);
				array[1] = this.conn.CreateParameter("@CardPassword", DbType.String, CardPassword);
				array[2] = this.conn.CreateParameter("@CardType", DbType.Int32, CardType);
				array[3] = this.conn.CreateParameter("@CardAgentName", DbType.String, CardAgentName);
				array[4] = this.conn.CreateParameter("@CardValidNum", DbType.String, CardValidNum);
				array[5] = this.conn.CreateParameter("@CardMoney", DbType.Decimal, CardMoney);
				array[6] = this.conn.CreateParameter("@CardValidUnit", DbType.String, CardValidUnit);
				array[7] = this.conn.CreateParameter("@CardEndDate", DbType.DateTime, CardEndDate);
				array[8] = this.conn.CreateParameter("@CardCreateTime", DbType.DateTime, dateTime);
				this.conn.Execute("INSERT INTO [HL_Card] ([CardType], [CardNum], [CardPassword], [CardMoney], [CardValidNum], [CardAgentName], [CardValidUnit], [CardEndDate], [CardState], [CardCreateTime]) VALUES (@CardType, @CardNum, @CardPassword, @CardMoney, @CardValidNum, @CardAgentName, @CardValidUnit, @CardEndDate, 0, @CardCreateTime)", array);
				num++;
			}
			return num;
		}
		public bool UpdateCard(string CardNum, string CardPassword, int CardType, string CardAgentName, string CardValidNum, decimal CardMoney, string CardValidUnit, DateTime CardEndDate, int CardID)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@CardPassword", DbType.String, CardPassword),
				this.conn.CreateParameter("@CardType", DbType.Int32, CardType),
				this.conn.CreateParameter("@CardAgentName", DbType.String, CardAgentName),
				this.conn.CreateParameter("@CardValidNum", DbType.String, CardValidNum),
				this.conn.CreateParameter("@CardMoney", DbType.Decimal, CardMoney),
				this.conn.CreateParameter("@CardValidUnit", DbType.String, CardValidUnit),
				this.conn.CreateParameter("@CardEndDate", DbType.DateTime, CardEndDate),
				this.conn.CreateParameter("@Original_CardID", DbType.Int32, CardID)
			};
			int num = this.conn.Execute("UPDATE [HL_Card] SET [CardType] = @CardType, [CardPassword] = @CardPassword, [CardMoney] = @CardMoney, [CardValidNum] = @CardValidNum, [CardAgentName] = @CardAgentName, [CardValidUnit] = @CardValidUnit, [CardEndDate] = @CardEndDate WHERE (([CardID] = @Original_CardID))", parameters);
			return num == 1;
		}
		public DataTable GetCardByStatus(int Status, string UserName)
		{
			string sql = "";
			switch (Status)
			{
			case 0:
				sql = "SELECT * FROM HL_Card WHERE CardState=0 and CardEndDate>getdate() ";
				break;
			case 1:
				sql = "SELECT * FROM HL_Card WHERE CardState=1 AND CardUserName='" + UserName + "'";
				break;
			case 2:
				sql = "SELECT * FROM HL_Card WHERE CardState=2 AND CardUserName='" + UserName + "'";
				break;
			case 3:
				sql = "SELECT * FROM HL_Card WHERE (CardState=3 or CardEndDate<getdate() ) AND CardUserName='" + UserName + "'";
				break;
			}
			return this.conn.GetDataTable(sql);
		}
		public bool UpdateCard(int CardID, string UserName)
		{
			IDbDataParameter[] parameters = new IDbDataParameter[]
			{
				this.conn.CreateParameter("@CardID", DbType.String, CardID),
				this.conn.CreateParameter("@UserName", DbType.String, UserName),
				this.conn.CreateParameter("@CardUseTime", DbType.DateTime, DateTime.Now)
			};
			int num = this.conn.Execute("UPDATE [HL_Card] SET  [CardUseTime] = @CardUseTime, [CardUserName] = @UserName, CardState=1 WHERE ([CardID] = @CardID)", parameters);
			return num == 1;
		}
		public bool DeleteCard(int CardID)
		{
			int num = this.conn.Execute("Delete From [HL_Card]  Where CardID=" + CardID);
			return num == 1;
		}
	}
}
